﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Transactions;
using BuscadorDePreco.DataContract;

namespace BuscadorDePreco.Data
{
    public class SegmentoTableAdapter
    {
        public int AdicionarSegmento(SegmentoData segmento)
        {
            TransactionOptions transactionConfig = new TransactionOptions();
            transactionConfig.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            int id = -1;
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionConfig))
            {
                using (BuscadorEntities buscadorEntities = new BuscadorEntities())
                {
                    Segmento seg = SegmentoData.ToSegmentoObj(segmento);

                    buscadorEntities.Segmento.AddObject(seg);
                    buscadorEntities.SaveChanges();

                    id = seg.Id;
                }
                scope.Complete();
            }

            return id;
        }

        public List<SegmentoData> RecuperarSegmentos()
        {
            List<SegmentoData> segmentosList = new List<SegmentoData>();

            using (BuscadorEntities buscadorEntities = new BuscadorEntities())
            {
                var segmentos = (from emp in buscadorEntities.Segmento
                              select emp).ToList();

                foreach (Segmento seg in segmentos)
                    segmentosList.Add(SegmentoData.ToSegmentoData(seg));
            }

            return segmentosList;
        }

        public void ExcluirSeguimento(int id)
        {
            TransactionOptions transactionConfig = new TransactionOptions();
            transactionConfig.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionConfig))
            {
                using (BuscadorEntities buscadorEntities = new BuscadorEntities())
                {
                    var segmento = (from seg in buscadorEntities.Segmento
                                    where seg.Id == id
                                    select seg).ToList();

                    if (segmento != null && segmento.Count > 0)
                    {
                        buscadorEntities.Segmento.DeleteObject(segmento.First());
                        buscadorEntities.SaveChanges();
                    }
                }
                scope.Complete();
            }
        }

        public SegmentoData RecuperaSegmento(int id)
        {
            using (BuscadorEntities buscadorEntities = new BuscadorEntities())
            {
                var segmentos = (from seg in buscadorEntities.Segmento
                                 where seg.Id == id
                                 select seg);

                if (segmentos != null && segmentos.Count() > 0)
                    return SegmentoData.ToSegmentoData(segmentos.First());
            }

            return null;
        }

        public void AtualizarSegmento(SegmentoData newData)
        {
            TransactionOptions transactionConfig = new TransactionOptions();
            transactionConfig.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionConfig))
            {
                using (BuscadorEntities buscadorEntities = new BuscadorEntities())
                {
                    var segmento = from seg in buscadorEntities.Segmento
                                    where seg.Id == newData.Id
                                    select seg;

                    if (segmento != null && segmento.Count() > 0)
                    {
                        Segmento c = segmento.First();
                        c.Nome = newData.Nome;
                    }

                    buscadorEntities.SaveChanges();

                }
                scope.Complete();
            }
        }
    }
}